/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.JsonResult;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.Record_task;
import com.thinkgem.jeesite.modules.cms.entity.TaskGroup;
import com.thinkgem.jeesite.modules.cms.entity.taskData;
import com.thinkgem.jeesite.modules.cms.entity.taskGroupData;
import com.thinkgem.jeesite.modules.cms.service.RecordService_task;
import com.thinkgem.jeesite.modules.cms.service.TaskGroupService;
import com.thinkgem.jeesite.modules.cms.service.TaskOperationService;

// TODO: Auto-generated Javadoc
/**
 * 合并任务描述Controller.
 *
 * @author leo
 * @version 2017-02-20
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/taskGroup")
public class TaskGroupController extends BaseController {

	/** The task group service. */
	@Autowired
	private TaskGroupService taskGroupService;
	@Autowired
	private RecordService_task record_taskService;
	@Autowired
	private TaskOperationService taskOperationService;
	/**
	 * Gets the.
	 *
	 * @param id the id
	 * @return the task group
	 */
	
	@ModelAttribute
	public TaskGroup get(@RequestParam(required=false) String id) {
		TaskGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = taskGroupService.get(id);
		}
		if (entity == null){
			entity = new TaskGroup();
		}
		return entity;
	}
	
	/**
	 * List.
	 *
	 * @param taskGroup the task group
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @return the string
	 */
	@RequiresPermissions("cms:taskGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(TaskGroup taskGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TaskGroup> page = taskGroupService.findPage(new Page<TaskGroup>(request, response), taskGroup); 
		model.addAttribute("page", page);
		return "modules/cms/taskGroupList";
	}

	/**
	 * Form.
	 *
	 * @param taskGroup the task group
	 * @param model the model
	 * @return the string
	 */
	@RequiresPermissions("cms:taskGroup:view")
	@RequestMapping(value = "form")
	public String form(TaskGroup taskGroup, Model model) {
		model.addAttribute("taskGroup", taskGroup);
		return "modules/cms/taskGroupForm";
	}

	/**
	 * Save.
	 *
	 * @param taskGroup the task group
	 * @param model the model
	 * @param redirectAttributes the redirect attributes
	 * @return the string
	 */
	@RequiresPermissions("cms:taskGroup:edit")
	@RequestMapping(value = "save")
	public String save(TaskGroup taskGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, taskGroup)){
			return form(taskGroup, model);
		}
		taskGroupService.save(taskGroup);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
	}
	
	/**
	 * Delete.
	 *
	 * @param taskGroup the task group
	 * @param redirectAttributes the redirect attributes
	 * @return the string
	 */
	@RequiresPermissions("cms:taskGroup:delete")
	@RequestMapping(value = "delete")
	public String delete(TaskGroup taskGroup, RedirectAttributes redirectAttributes) {
		taskGroupService.delete(taskGroup);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/taskGroup/?repage";
	}
	
	
	/**
	 * Gets the by date to print.
	 *  .../cms/taskGroup/getTaskGroup
	 * @param date1 the date 1
	 * @param date2 the date 2
	 * @return the by date to print
	 */
	@RequestMapping(value = { "/getTaskGroup" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST} )
	@ResponseBody
	public List<TaskGroup> getByDateToPrint(HttpServletRequest request,
			HttpServletResponse response,String date1 ,String date2){
//		JsonResult result = new JsonResult();
//		if (date1.equals("") && date2.equals("")) {
//			result.setRet_code(-1);
//		} else {
//			result.setData(this.taskGroupService.getByDateToPrint(date1 , date2));
//			result.setRet_code(0);
//		}
		return this.taskGroupService.getByDateToPrint(date1 , date2);
		
	}
	

	@RequestMapping(value = { "/getById" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET} )
	@ResponseBody
	public JsonResult getById(HttpServletRequest request,
			HttpServletResponse response,String id,String userid) {
		JsonResult result = new JsonResult();
		taskGroupData tgdata =  null;
		if (StringUtils.isNotBlank(id)&& StringUtils.isNotBlank(userid)){
			tgdata = taskGroupService.getById(id,userid);
		}
		if (tgdata != null) {
			result.setData(tgdata);
			result.setRet_code(0);
		} else {
			result.setRet_code(-1);
		}
		return result;
	}
	
	/**
	 * 合并id 用户id 未完成 
	 * @param keyfield
	 * @param userid
	 * @param force
	 * @return
	 */
	@RequestMapping(value = { "/start" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult start(String keyfield, String userid, int force) {
		JsonResult result = new JsonResult();
		List<String> q = this.taskGroupService.checkIsok(keyfield, userid);
		if (q.size()>0){
			result.setData(q);
		}
		int r = this.taskGroupService.getstart(keyfield, userid);
		if (r ==0)
		{
			result.setRet_code(-1);
			return result;
		}
		if(force >=r){
			this.taskGroupService.start(keyfield, userid);
			Record_task record = new Record_task();
			Date date = new Date();
	
			record.setKeyfield(keyfield);
			record.setDate(date);
			record.setTime(date);
			record.setType("GS");//合并
			record.setUserid(userid);
			record.setDescription(userid + "-开工");
			this.record_taskService.save(record);
//			List<taskData> list = this.taskOperationService.getByGroupId(keyfield);
//			for (taskData t : list) {
//				this.taskOperationService.start(t.getKeyfield(), userid);
//			}
			
			result.setRet_code(0);
		}else{
			result.setRet_code(1);
		}
		return result;
	}

	/**
	 * Complete.
	 *
	 * @param keyfield the keyfield
	 * @param userid the userid
	 * @param force the force
	 * @return the json result
	 */
	@RequestMapping(value = { "/complete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JsonResult complete(String keyfield, String userid, int force) {
		JsonResult result = new JsonResult();
		List<String> q = this.taskGroupService.checkIsok(keyfield, userid);
		if (q.size()>0){
			result.setData(q);
		}
		//任务完成时不判断产品合格状态  a.taskgroupid=#{groupid} and a.userid=#{userid} and a.istaskcomplete=0 and a.isstart=1
		int r = this.taskGroupService.getcomplete(keyfield, userid);
		if (r ==0)
		{
			result.setRet_code(-1);
			return result;
		}
		if(force >=r){
			this.taskGroupService.complete(keyfield, userid);
			Record_task record = new Record_task();
			Date date = new Date();
	
			record.setKeyfield(keyfield);
			record.setDate(date);
			record.setTime(date);
			record.setType("GE");//合并任务完成
			record.setUserid(userid);
			record.setDescription(userid + "-完工");
			this.record_taskService.save(record);
			
			
			result.setRet_code(0);
		}else{
			result.setRet_code(1);
		}
		return result;
	}

}